$(function() {
	//datepicker();
	slayerInfo();

	$('#protectionMainBody_form')
			.validate(
					{
						errorElement : 'div',
						errorClass : 'help-block',
						focusInvalid : true,
						ignore : "",
						rules : {
							
							// gis_area : {
							// required : true,
							// maxlength : 100
							// },
//							sectorization_mapserver : {
//								required : true,
//								maxlength : 100
//							},
//							sectorization_layer : {
//								required : true,
//								maxlength : 100
//
//							},
							
						},

						messages : {
//							name : {
//								required : "<span style='color: red;'>请输入名称！</span>",
//								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
//							},
//							official_area : {
//								required : "<span style='color: red;'>请输入官方面积！</span>",
//								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
//							},
//							// gis_area : {
//							// required : "<span style='color:
//							// red;'>请输入一些描述！</span>",
//							// maxlength : "<span style='color:
//							// red;'>最大长度不能超过500个字符！</span>"
//							// },
//							location_gis_mapserver : {
//								required : "<span style='color: red;'>请选择Mapserver！</span>",
//								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
//							},
//							location_gis_layer : {
//								required : "<span style='color: red;'>请选择图层！</span>",
//								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
//							},
//							location_des : {
//								required : "<span style='color: red;'>请输入地理区位！</span>",
//								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
//							},
//							border_des : {
//								required : "<span style='color: red;'>请输入边界描述！</span>",
//								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
//							}
						},

						highlight : function(e) {
							$(e).closest('.form-group').removeClass('has-info')
									.addClass('has-error');
							protectionMainBody.html(protectionMainBody.text() + warnHTML);
						},

						success : function(e) {
							$(e).closest('.form-group')
									.removeClass('has-error').addClass(
											'has-info');
							// $(e).remove();
						}
					});

	$("#protectionMainBody_btn").click(function() {
		protectionMainBody_add();
	});
	datepicker();
});


function datepicker() {
	$.fn.datetimepicker.dates = {
		cn : {
			days : [ "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" ],
			daysShort : [ "日", "一", "二", "三", "四", "五", "六", "日" ],
			daysMin : [ "日", "一", "二", "三", "四", "五", "六", "日" ],
			months : [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月",
					"十月", "十一月", "十二月" ],
			monthsShort : [ "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
					"十一", "十二" ]
		/*
		 * today: "今天", clear: "取消"
		 */
		}
	};
	var dates = $.fn.datetimepicker.dates;
	$('#build_date').datetimepicker({
		defaultDate : new Date()
	});

	

}



function slayerInfo() {
	dojo.require("esri.map");
	$("#sectorization_layer").empty();
	var layer = [];
	var mapserver = $("#sectorization_mapserver").val();

	$.ajax({
		url : "findmapserver",
		dataType : "json",
		type : "POST",
		data : {mapserver:mapserver},
		async : false,
		error : function(error) {
			console.log(error.responseText);

			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});

		},
		success : function(strValue) {
			if (strValue.length != 0) {

				$("#sectorization_layer").empty();

				for ( var i in strValue) {
					var option = "<option value=" + strValue[i] + ">"
							+ strValue[i] + "</option>";
					$("#sectorization_layer").append(option);
				}

			}else{
				$("#sectorization_mapserver").empty();
				$("#sectorization_layer").empty();
				$("#sectorization_mapserver").append("<option value=' '>请选择</option>");
				$("#sectorization_layer").append("<option value=' '>请选择</option>");
			}
		}
	});
	
	
//	layer = new esri.layers.ArcGISDynamicMapServiceLayer(mapserver);
//	if (layer.loaded) {
//		buildLayerList(layer);
//	} else {
//		dojo.connect(layer, "onLoad", buildLayerLists);
//	}
//	function buildLayerLists(layer) {
//		var layerinfos = layer.layerInfos;
//		if (layerinfos != null && layerinfos.length > 0) {
//			for (var i = 0, j = layerinfos.length; i < j; i++) {
//				var info = layerinfos[i];
//				info.defaultVisibility == false;
//				var option = "<option value=" + info.name + ">" + info.name
//						+ "</option>";
//				$("#sectorization_layer").append(option);
//			}
//		}
//		// map.addLayer(layer);
//	}
}

function sgismap() {
	$("#sectorization_map").empty();
	var filepath = $("#sectorization_layer").val();
	var map;
	var layer;
	
	dojo.require("dijit.layout.BorderContainer");
	dojo.require("dijit.layout.ContentPane");
	dojo.require("esri.map");
	function init() {

		map = new esri.Map("sectorization_map",{
			basemap : "streets",
			slider : true
		});
		//var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer");
        //map.addLayer(basemap);
		
		var point = new esri.geometry.Point(longitude,
				latitude);
		map.centerAndZoom(point, scale);
		layer = new esri.layers.ArcGISDynamicMapServiceLayer(filepath);

		if (layer.loaded) {
			buildLayerList(layer);
		} else {
			dojo.connect(layer, "onLoad", buildLayerList);
		}

	}
	function buildLayerList(layer) {
		var layerinfos = layer.layerInfos;
		var visible = [];
		var visiblelayer = [];
		if (layerinfos != null && layerinfos.length > 0) {
			
			for (var i = 0, j = layerinfos.length; i < j; i++) {
			var info = layerinfos[i];
			info.defaultVisibility == true;
//			var option = "<option value=" + info.name + ">" + info.name
//					+ "</option>";
//			$("#location_gis_layer").append(option);
			visible.push(info.id);
			}
			
			
			if (visible.length == 0) {
				visible.push(-1);
			}
			layer.setVisibleLayers(visible);
		}
		map.addLayer(layer);
	}
	dojo.ready(init);
}

function protectionMainBody_add() {
	var option = {
		dataType : 'json',
		data : {
			wetlandParkId : wetlandParkId
		},
		success : function(value) {
			if (value == true) {
				protectionMainBody.html(protectionMainBody.text() + successHTML);
				$.gritter.add({
					title : '确认消息',
					text : '恭喜您,信息添加成功！',
					class_name : 'gritter-success'
				});
			} else {
				protectionMainBody.html(protectionMainBody.text() + warnHTML);
				$.gritter.add({
					title : '确认消息',
					text : '对不起,信息添加失败！',
					class_name : 'gritter-error'
				});
			}
		},
		error : function() {
			protectionMainBody.html(protectionMainBody.text() + warnHTML);
			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});
		}
	};
	$("#protectionMainBody_form").ajaxForm(option).submit();

}